package com.lexian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lexian.entity.Customer;
import com.lexian.mapper.CustomerMapper;
import com.lexian.service.CustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lexian.utils.CommonResponse;
import com.lexian.utils.JwtUtils;
import com.lexian.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 顾客 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-01-14
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Autowired
    public CustomerService CustomerService;

    /**
     * 顾客注册(登录) 返回jwt规则生成封装用户信息的token字符串
     * @return
     */
    @Override
    public String login(Customer customer) {

        String phone = customer.getPhone();//手机号
        String password = customer.getPassword();//密码

        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)){
            throw new RuntimeException("手机号或密码不能为空");
        }

        //登录
        Customer Customer = isExistsByPhone(phone);
        if(Customer==null){
            throw new RuntimeException("没有这个手机号");
        }

        //判断密码是否正确
        if(!MD5.encrypt(password).equals(Customer.getPassword())){
            throw new RuntimeException("密码错误");
        }

        //判断用户是否禁用
        if(Customer.getStatus()==0){
            throw new RuntimeException("用户已禁用");
        }
        //根据id和昵称生成token字符串
        String token = JwtUtils.getJwtToken(Customer.getCusId().toString(), Customer.getNickname());


        return token;
    }

    /**
     * 注册
     * @param customer
     * @return
     */
    @Override
    public CommonResponse register(Customer customer){
        CommonResponse commonResponse=null;
        //用户注册信息
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("Customer",customer);
        String phone=customer.getPhone();//手机号码
        String password=customer.getPassword();//密码
        //手机号码是否已绑定账号
        if(isExistsByPhone(phone)==null){
            if(baseMapper.insert(customer)>0){//注册成功
                commonResponse=CommonResponse.ok();
                commonResponse.setData(data);
                return commonResponse;
            }else{//注册失败
                commonResponse=CommonResponse.error();
                commonResponse.setData(data);
                return commonResponse;
            }
        }else{//该手机号码已有绑定账户
            commonResponse=CommonResponse.error();
            commonResponse.setData(data);
            commonResponse.setMessage("该手机号码已有绑定账户");
            return commonResponse;
        }
    }

    /**
     * 手机号是否已绑定用户
     * @return
     */
    @Override
    public Customer isExistsByPhone(String phone){
        QueryWrapper<Customer> wrapper = new QueryWrapper<Customer>();
        wrapper.eq("phone",phone);
        Customer Customer = baseMapper.selectOne(wrapper);
        if(Customer==null){
            return null;
        }
        return Customer;
    }
}
